//
//  UploadCustomHead.m
//  Unity-iPhone
//
//  Created by zhuwuyi on 2018/3/30.
//

#import <Foundation/Foundation.h>

#import "UploadCustomHead.h"

@interface UploadCustomHead ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property(strong,nonatomic)UIImagePickerController *picker;
@property(strong,nonatomic)UIViewController *vc;
@end

@implementation UploadCustomHead
@synthesize popoverViewController = _popoverViewController;

- (void)viewDidLoad {
    [super viewDidLoad];
    if(_picker!= nil)
        _picker.delegate =self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showPicker:(UIImagePickerControllerSourceType)type
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = type;
    picker.allowsEditing = YES;
    
    // We need to display this in a popover on iPad
    if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
    {
        Class popoverClass = NSClassFromString( @"UIPopoverController" );
        if( !popoverClass )
            return;

        _popoverViewController = [[popoverClass alloc] initWithContentViewController:picker];
        [_popoverViewController setDelegate:self];
        [_popoverViewController presentPopoverFromRect:CGRectMake( 0, 0, 128, 128 )
                                                inView:UnityGetGLViewController().view
                              permittedArrowDirections:UIPopoverArrowDirectionAny
                                              animated:YES];
    }
    else
    {
        UIViewController *vc = UnityGetGLViewController();
        NSLog(@"delegate1 = %@",picker.delegate);
        [vc presentViewController:picker animated:YES completion:^{
            picker.delegate = self;
            NSLog(@"_imagePicker.delegate2 = %@",picker.delegate);}];

       NSLog(@"delegate3 = %@",picker.delegate);
//        [vc presentViewController:picker animated:YES completion:^{
//            picker.delegate = self;
//        }];

    }
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController*)popoverController
{
    self.popoverViewController = nil;
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
//- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
    UIImage *image;
    UIImage *image2;
    image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    UIGraphicsBeginImageContext(CGSizeMake(128,128));
    [image drawInRect:CGRectMake(0, 0, 128, 128)];
    image2 = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSString *imagePath = [self GetSavePath:@"image.png"];
    [self SaveFileToDoc:image2 path:imagePath];
    [self dismissWrappedController];
}

-(NSString *)GetSavePath : (NSString *)filename{
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = [pathArray objectAtIndex:0];
    
    return [docPath stringByAppendingPathComponent:filename];
}

-(void) SaveFileToDoc:(UIImage *)image path:(NSString *)path{
    NSData *data;
    if(UIImagePNGRepresentation(image) == nil)
    {
        data = UIImagePNGRepresentation(image);
    }
    else
    {
        data = UIImagePNGRepresentation(image);
    }
    [data writeToFile:path atomically:YES];
    UnitySendMessage( "PlatformUtils", "onPickPhoto", "image.png");
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
    [self dismissWrappedController];

}

- (void)dismissWrappedController
{
    UIViewController *vc = UnityGetGLViewController();

    if( !vc )
        return;
    [vc dismissViewControllerAnimated:YES completion:nil];
    [self performSelector:@selector(removeAndReleaseViewControllerWrapper) withObject:nil afterDelay:1.0];
}

- (void)removeAndReleaseViewControllerWrapper
{
    if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && _popoverViewController )
    {
        [_popoverViewController dismissPopoverAnimated:YES];
        self.popoverViewController = nil;
    }
}
@end


extern "C"
{
    void openPhoto()//相册
    {
        static UploadCustomHead *app = [[UploadCustomHead alloc] init];
        [app showPicker:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    
}
